home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / StyledEdit / source code / StyledEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-27  |  4.4 KB  |  175 lines  |  [TEXT/CWIE]

  1.  
  2.  
  3. /*
  4.     This MacHack implements styled text in Metrowerk's (and BBEdit's) Editing Windows.
  5.     by Matthew Xavier Mora mxmora@apple.com
  6.  
  7.     Because of the fact that both Metrowerks and BBedit draw and measure text differently
  8.     this is only a visible hack. You don't really want to use this for real work since
  9.     hit testing is off for lines of text that have been modified. Also this is a 68k
  10.     patch on what is most likely a native trap.
  11.  
  12.     (6-27-97)
  13. */
  14.  
  15. #ifndef THINK_C 
  16. #    ifndef __MWERKS__
  17. #        error unknown compiler
  18. #    endif
  19. #endif
  20.  
  21. //#define OLDROUTINENAMES 0
  22. #define SystemSevenFiveOrLater 1
  23.  
  24. #define kMyID  (128)
  25.  
  26. //#include <strings.h>
  27. //#include <Resources.h>
  28. #include <Memory.h>
  29. #include <Events.h>
  30. #include <SetUpA4.h>
  31. #include <Processes.h>
  32. #include <ToolUtils.h>
  33. #include <Traps.h>
  34. //#include <Notification.h>
  35. #include <Windows.h>
  36. #include <Gestalt.h>
  37. #include <LowMem.h>
  38.  
  39. #include "ShowInitIcon.h"
  40. #include "StyledEdit.h"
  41.  
  42. QDTextUPP        oldStdText;
  43. QDTxMeasUPP        oldTextMeasure;
  44.  
  45. MyGlobalsPtr         globs;
  46. short             err;
  47.  
  48. pascal void NewStdText(short count,char *textAddr,Point numer,Point denom);
  49. pascal short NewTextMeasure(short count, Ptr textAddr, Point *numer, Point *denom, FontInfo *info);
  50.  
  51. //-----------------------------------------------------------------------------------------------
  52. pascal void NewStdText(short count,char *textAddr,Point numer,Point denom)
  53. //-----------------------------------------------------------------------------------------------
  54. {
  55.     long         oldA4 = SetUpA4();
  56.     Ptr             oldAddr = textAddr;
  57.     short         oldFace;
  58.     CGrafPtr        theWindow ;
  59.     RGBColor         theColor;
  60.     Boolean         alreadyCalled;
  61.  
  62.     alreadyCalled = false;
  63.  
  64.     if (globs->enabled) {
  65.         //offset = contains(textAddr, srcLen, findStr, 0);
  66.         //theWindow = (CGrafPtr)FrontWindow();
  67.         GetPort(&(GrafPtr)theWindow);
  68.         if (theWindow) {
  69.  
  70.             GetForeColor(&theColor);
  71.             if (theColor.red == 0 && theColor.green == 0 && theColor.blue  ) { // if kinda blue
  72.  
  73.                 // set the face
  74.                 oldFace = theWindow->txFace;
  75.                 TextFace(bold + condense);
  76.                 CallQDTextProc(oldStdText, count, textAddr, numer, denom);
  77.                 TextFace(oldFace);
  78.                 alreadyCalled = true;
  79.             }
  80.         }
  81.     }
  82.     if (!alreadyCalled) {
  83.         CallQDTextProc(oldStdText, count, textAddr, numer, denom);
  84.     }
  85.  
  86.     RestoreA4(oldA4);
  87. }
  88.  
  89. //-----------------------------------------------------------------------------------------------
  90.     pascal short NewTextMeasure(short count, Ptr textAddr, Point *numer, Point *denom, FontInfo *info)
  91. //-----------------------------------------------------------------------------------------------
  92. {
  93.     long         oldA4 = SetUpA4();
  94.     short         result;
  95.     short         oldFace;
  96.     CGrafPtr        theWindow ;
  97.     RGBColor         theColor;
  98.     Boolean         alreadyCalled;
  99.  
  100.     alreadyCalled =false;
  101.     if (globs->enabled) {
  102.         GetPort(&(GrafPtr)theWindow);
  103.         if (theWindow) {
  104.             GetForeColor(&theColor);
  105.             if (theColor.red == 0 && theColor.green == 0 && theColor.blue  ) { // if it is sorta blue
  106.                 // set the face to bold condense
  107.                 oldFace = theWindow->txFace;
  108.                 TextFace(bold + condense);
  109.                 result = CallQDTxMeasProc(oldTextMeasure,count,textAddr,numer,denom,info);
  110.                 TextFace(oldFace);
  111.                 alreadyCalled = true;
  112.             } else if (theColor.red && theColor.green == 0 && theColor.blue  ==0  ) {
  113.                 // set the face to italic
  114.                 oldFace = theWindow->txFace;
  115.                 TextFace(italic);
  116.                 result = CallQDTxMeasProc(oldTextMeasure,count,textAddr,numer,denom,info);
  117.                 TextFace(oldFace);
  118.                 alreadyCalled = true;
  119.             }
  120.         }
  121.     }
  122.  
  123.     if (!alreadyCalled) {
  124.         result = CallQDTxMeasProc(oldTextMeasure,count,textAddr,numer,denom,info);
  125.     }
  126.     
  127.     RestoreA4(oldA4);
  128.     return result;
  129. }
  130.  
  131. #if defined (__MWERKS__)
  132. #    include <A4Stuff.h>
  133. #elif defined (THINK_C)
  134.     pascal void * GetA0 (void) = { 0x2E88 };
  135. #endif
  136.  
  137. void main (void) {
  138. #if defined (__MWERKS__)
  139.     void __Startup__ (void);
  140.     long oldA4 = SetCurrentA4 ( );
  141.     RememberA4 ( );
  142.     DetachResource (RecoverHandle ((Ptr) __Startup__));
  143. #elif defined (THINK_C)
  144.     void *me = GetA0 ( );
  145.     RememberA0 ( );
  146.     DetachResource (RecoverHandle (me));
  147.     SetUpA4 ( );
  148. #endif
  149.     
  150.     globs = (MyGlobalsPtr)NewPtrSysClear(sizeof(MyGlobals));
  151.  
  152.     if (globs) {
  153.         globs->enabled = true;
  154.     }
  155.  
  156.     err = NewGestaltValue(kStyledEditSig,(long)globs);
  157.  
  158.     oldStdText = (QDTextUPP)GetToolboxTrapAddress(_StdText);
  159.     SetToolboxTrapAddress((UniversalProcPtr)NewQDTextProc(NewStdText),_StdText);
  160.  
  161.     oldTextMeasure = (QDTxMeasUPP)GetToolboxTrapAddress(_StdTxMeas);
  162.     SetToolboxTrapAddress((UniversalProcPtr)NewQDTxMeasProc(NewTextMeasure),_StdTxMeas);
  163.  
  164.     ShowInitIcon (kMyID, true);
  165.  
  166. #if defined (__MWERKS__)
  167.     SetA4 (oldA4);
  168. #elif defined (THINK_C)
  169.     RestoreA4 ( );
  170. #endif
  171. }
  172.  
  173.  
  174.  
  175.